<!DOCTYPE html>
<html lang="zh-CN" xmlns:th="http://www.thymeleaf.org">

<head th:replace="fregments :: head(~{::title})">
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <title>个人中心</title>
    <!-- Bootstrap -->
    <link rel="stylesheet" href="../static/lib/bootstrap-3.4.1/css/bootstrap.min.css">
    <link rel="stylesheet" href="../static/css/my.css">
    <!-- 动画 -->
    <link rel="stylesheet" href="../static/css/animate.css">

</head>

<body>
<nav th:replace="fregments :: menu(0)" class="navbar navbar-default navbar-fixed-top m-padding">
    <div class="container-fluid">
        <!-- Brand and toggle get grouped for better mobile display -->
        <div class="navbar-header">
            <button type="button" class="navbar-toggle collapsed" data-toggle="collapse"
                    data-target="#bs-example-navbar-collapse-1" aria-expanded="false">
                <span class="sr-only">Toggle navigation</span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
                <span class="icon-bar"></span>
            </button>
            <a class="navbar-brand" href="#">
                <img alt="Brand" src="../static/favicon.ico" style="width: 24px;">
            </a>
            <p class="navbar-text navbar-default"><a href="#" class="navbar-link m-a-none"><b>挚夕导航</b></a></p>
        </div>

        <!-- Collect the nav links, forms, and other content for toggling -->
        <div class="collapse navbar-collapse" id="bs-example-navbar-collapse-1">
            <ul class="nav navbar-nav">
                <li class="active"><a href="#"><span class="glyphicon glyphicon-home">&nbsp;</span>首页</a></li>
                <li><a href="#"><span class="glyphicon glyphicon-pencil">&nbsp;</span>学习</a></li>
                <li role="presentation" class="disabled"><a href="#"><span
                        class="glyphicon glyphicon-bookmark">&nbsp;</span>个人书签</a></li>
            </ul>

            <ul class="nav navbar-nav navbar-right">
                <li class="dropdown">
                    <a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-haspopup="true"
                       aria-expanded="false">
                        <img class="m-avatar img-circle" src="../static/images/me.jpg">
                        挚夕
                        <span class="caret"></span>
                    </a>
                    <ul class="dropdown-menu dropdown-menu-left">
                        <li><a href="#"><span class="glyphicon glyphicon-th">&nbsp;</span>个人主页</a></li>
                        <li role="separator" class="divider"></li>
                        <li><a href="#"><span class="glyphicon glyphicon-log-out">&nbsp;</span>登出</a></li>
                    </ul>
                </li>
            </ul>

            <form class="navbar-form navbar-right">
                <div class="form-group">
                    <input type="text" class="form-control" placeholder="搜索" style="width: 300px;">
                </div>
                <button type="submit" class="btn btn-default">
                    <a class="glyphicon glyphicon-search m-none"></a>
                </button>
            </form>
        </div><!-- /.navbar-collapse -->
    </div><!-- /.container-fluid -->
</nav>
<th:block th:replace="fregments :: modal"></th:block>

<div id="way-point" class="animated fadeIn">
    <div class="m-container" id="myContainer">
        <!-- 内容开始 -->
        <div name="content" class="row">
            <div class="col-xs-12 col-sm-3 col-md-3 left-container">
                <div class="col-xs-12 col-sm-12 col-md-12">
                    <div class="panel panel-default ">
                        <div class="panel-heading" id="userAvatar">
                            <th:block th:fragment="userAvatar">
                                <img class="user-avatar" th:src="${user == null} ? @{/images/me.jpg} : ${user.avatar}"
                                     src="/static/images/me.jpg">
                            </th:block>
                        </div>
                        <div class="panel-body">
                            <div class="list-group">
                                <a id="userInfo" data-item="1" onclick="clickItem(this)" class="list-group-item"
                                   th:classappend="${item == 1} ? 'active'">
                                    个人资料
                                </a>
                                <a data-item="2" onclick="clickItem(this)" class="list-group-item"
                                   th:classappend="${item == 2} ? 'active'">
                                    站内消息
                                </a>
                                <a data-item="3" onclick="clickItem(this)" class="list-group-item"
                                   th:classappend="${item == 3} ? 'active'">
                                    我的收藏
                                </a>
                                <a data-item="4" onclick="clickItem(this)" class="list-group-item"
                                   th:classappend="${item == 4} ? 'active'">
                                    账户安全
                                </a>
                                <a class="list-group-item" onclick="logout()">
                                    安全退出
                                </a>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
            <!-- 右边div开始-->
            <div class="col-xs-12 col-sm-9 col-md-9 right-container">
                <div class="col-xs-12 col-sm-12 col-md-12">
                    <!-- 面板开始 -->
                    <div class="panel panel-default" id="content">
                        <th:block th:fragment="content">
                            <div class="panel-heading">
                                <th:block>
                                    <span th:if="${item == 1}" class="m-title active">个人资料</span>
                                    <span th:if="${item == 2}" class="m-title">站内消息</span>
                                    <span th:if="${item == 3}" class="m-title">我的收藏</span>
                                    <span th:if="${item == 4}" class="m-title">账户安全</span>
                                </th:block>
                            </div>
                            <div class="panel-body row">
                                <!-- 个人资料开始-->
                                <div th:if="${item == 1}" class="col-xs-12 col-sm-12 col-md-12" id="userInfoForm">
                                    <form>
                                        <div class="col-xs-12 col-sm-12 col-md-12 form-group"
                                             style="padding-left: 0!important;">
                                            <div class="col-xs-3 col-sm-2 col-md-2" style="padding-left: 0!important;">
                                                <span class="m-text">昵称</span>
                                            </div>
                                            <div class="col-xs-9 col-sm-10 col-md-10">
                                                <input type="text" class="form-control" id="nickname" name="nickname"
                                                       th:value="${user.nickname}">
                                            </div>
                                        </div>
                                        <div class="col-xs-12 col-sm-12 col-md-12 form-group"
                                             style="margin-top: 20px;padding-left: 0">
                                            <div class="col-xs-3 col-sm-2 col-md-2" style="padding-left: 0">
                                                <span class="m-text">用户名</span>
                                            </div>
                                            <div class="col-xs-9 col-sm-10 col-md-10">
                                                <span style="font-weight: bolder"
                                                      th:text="${user.username}">dylan</span>
                                            </div>
                                        </div>
                                        <div class="col-xs-12 col-sm-12 col-md-12 form-group"
                                             style="margin-top: 20px;padding-left:0">
                                            <div class="col-xs-3 col-sm-2 col-md-2" style="padding-left:0">
                                                <span class="m-text">头像</span>
                                            </div>
                                            <div class="col-xs-9 col-sm-10 col-md-10">
                                                <img id="selectAvatar" src="/static/images/me.jpg"
                                                     th:src="@{/images/me.jpg}"
                                                     class="m-avatar img-circle m-pointer">
                                                <input type="file" name="avatar" id="inputAvatar"
                                                       onchange="chooseImg(this)"
                                                       accept="image/jpg, image/png, image/jpeg" style="display: none">
                                                <input type="radio" name="avatar" id="radioSelectAvatar"> &nbsp;自定义
                                                <img src="/static/images/me.jpg" th:src="@{/images/me.jpg}"
                                                     class="m-avatar img-circle">
                                                <input type="radio" name="avatar" checked>&nbsp;默认
                                            </div>
                                        </div>
                                        <div class="col-xs-12 col-sm-12 col-md-12 form-group"
                                             style="margin-top: 20px;padding-left:0">
                                            <div class="col-xs-3 col-sm-2 col-md-2" style="padding-left:0">
                                                <span class="m-text">个人描述</span>
                                            </div>
                                            <div class="col-xs-9 col-sm-10 col-md-10">
                                            <textarea id="description" class="form-control" rows="10" name="description"
                                                      placeholder="欲买桂花同载酒，终不似少年游。">[[${description}]]</textarea>
                                            </div>
                                        </div>
                                        <div class="col-xs-12 col-sm-12 col-md-12"
                                             style="margin-top: 20px;padding-left:0">
                                            <div class="col-xs-3 col-sm-2 col-md-2" style="padding-left:0">

                                            </div>
                                            <div class="col-xs-9 col-sm-10 col-md-10">
                                                <button id="btnSaveInfo" type="button" class="btn btn-primary">保存资料
                                                </button>
                                            </div>
                                        </div>
                                    </form>
                                </div><!-- 个人资料结束-->
                                <!-- 站内消息开始-->
                                <div th:if="${item == 2}" class="col-xs-12 col-sm-12 col-md-12"><!-- 站内消息开始-->
                                    <div class="col-xs-12 col-sm-12 col-md-12" style="padding-left: 0!important;">
                                        <div class="form-group">
                                            <span>1998-07-07 13:14:34</span>
                                            <div class="well">
                                                发送者: System 欢迎来到挚夕导航，请首先在个人设置中完善您的账号信息。
                                            </div>
                                        </div>
                                    </div>
                                </div><!-- 站内消息结束-->
                                <!-- 我的收藏开始-->
                                <div th:if="${item == 3}" class="col-xs-12 col-sm-12 col-md-12"><!-- 我的收藏开始-->
                                    <div class="col-xs-12 col-sm-12 col-md-12" style="padding-left: 0!important;">
                                        <!-- 链接列表-->
                                        <!-- row start -->
                                        <div class="row">
                                            <div class="col-xs-12 col-sm-6 col-md-4" th:each="link : ${links}">
                                                <div class="m-card row animated lightSpeedIn">
                                                    <a class="m-padded-left-sm m-padded-none col-xs-11 col-sm-11 col-md-11 m-a-none"
                                                       th:href="@{/link/{id}(id=${link.id})}">
                                                        <!--                                    <div class="m-inline">-->
                                                        <div class="m-margin-top-sm m-padded-none col-xs-2 col-sm-2 col-md-2">
                                                            <img class="m-png img-circle"
                                                                 th:src="${link.icon}">
                                                        </div>
                                                        <div class="m-padded-none m-padded-top-sm col-xs-10 col-sm-10 col-md-10">
                                                            <div>
                                                                <strong th:text="${link.title}">哔哩哔哩</strong>
                                                            </div>
                                                            <!--                                            #strings.substring(link.brief, 0, #strings.length(link.brief) > 8 ? 8 : #strings.length(link.brief)) + '...'-->
                                                            <div class="text-muted m-brief-control m-inline"
                                                                 th:text="${link.brief}">
                                                                哔哩哔哩干杯
                                                            </div>
                                                        </div>
                                                        <!--                                    </div>-->
                                                    </a>
                                                    <a class="m-margin-top-big m-padded-none col-xs-1 col-sm-1 col-md-1 m-a-none"
                                                       th:href="${link.url}" target="_blank"
                                                       data-toggle="tooltip" data-placement="right"
                                                       data-container="body"
                                                       title="直达">
                                                        <span class="glyphicon glyphicon-chevron-right">&nbsp;</span>
                                                    </a>
                                                </div>
                                            </div>
                                        </div> <!-- row end -->
                                    </div>
                                </div><!-- 我的收藏结束-->
                                <!-- 账户安全开始-->
                                <div th:if="${item == 4}" class="col-xs-12 col-sm-12 col-md-12">
                                    <div class="col-xs-12 col-sm-12 col-md-12" style="padding-left: 0!important;">
                                        <div class="col-xs-12 col-sm-12 col-md-12" style="padding-left: 0!important;">
                                            <div class="col-xs-3 col-sm-2 col-md-2" style="padding-left: 0!important;">
                                                <span class="m-text">绑定邮箱</span>
                                            </div>
                                            <div class="col-xs-9 col-sm-10 col-md-10">
                                                <div class="well well-sm">
                                                    <span class="glyphicon glyphicon-envelope"></span>
                                                    <span>
                                                        已经绑定
                                                        <span th:text="${email}"></span>
                                                    </span>
                                                </div>
                                            </div>
                                        </div>
                                    </div>
                                    <div class="col-xs-12 col-sm-12 col-md-12" style="padding-left: 0!important;">
                                        <div class="col-xs-12 col-sm-12 col-md-12" style="padding-left: 0!important;">
                                            <div class="col-xs-3 col-sm-2 col-md-2" style="padding-left: 0!important;">
                                                <span class="m-text">设置密码</span>
                                            </div>
                                            <div class="col-xs-9 col-sm-10 col-md-10">
                                                <button data-toggle="modal" data-target="#myModal" type="button"
                                                        class="btn btn-danger">修改密码
                                                </button>
                                            </div>
                                        </div>
                                    </div>
                                </div><!-- 账户安全结束-->
                            </div>
                        </th:block>
                    </div><!-- 面板结束 -->
                </div><!-- 右边div结束-->
            </div>
        </div>
    </div>
</div>

<!--修改密码模态框-->
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModal">
    <div class="modal-dialog" role="document">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                        aria-hidden="true">&times;</span></button>
                <h4 class="modal-title">修改密码</h4>
            </div>
            <div class="modal-body">
                <form autocomplete="on">
                    <div class="form-group has-feedback">
                        <input type="password" class="form-control fc-clear" name="oldPassword" id="oldPassword"
                               placeholder="原密码">
                        <span class="m-eye">
                                <span class="eye glyphicon glyphicon-eye-open form-control-feedback m-pointer"></span>
                            </span>
                    </div>
                    <div class="form-group has-feedback">
                        <input type="password" class="form-control fc-clear" name="newPassword1" id="newPassword1"
                               placeholder="新密码">
                        <span class="m-eye">
                                <span class="eye glyphicon glyphicon-eye-open form-control-feedback m-pointer"></span>
                            </span>
                    </div>
                    <div class="form-group has-feedback">
                        <input type="password" class="form-control fc-clear" name="newPassword2" id="newPassword2"
                               placeholder="确认密码">
                        <span class="m-eye">
                                <span class="eye glyphicon glyphicon-eye-open form-control-feedback m-pointer"></span>
                            </span>
                    </div>
                </form>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                <button type="button" class="btn btn-primary" id="btn-sub">提交</button>
            </div>
        </div>
    </div>
</div>

<th:block th:if="false">
    <h1 th:fragment="notLogin">请先登录</h1>
</th:block>
<!-- 剪裁图片-->
<div id="clipping-container"></div>
<br><br><br><br><br><br><br><br><br>
<footer th:replace="_fregments :: footer">
    <div>
        <div class="row">
            <div class="col-sm-4 col-md-4">
                <h3>挚夕导航</h3>
                <p>
                    挚夕导航最初源于本人想要做一个管理书签的系统。<br>
                    后来借鉴了一些导航网站，决定做一个网站导航。<br>
                    前端使用BootStrap。
                </p>
            </div>
            <div class="col-sm-8 col-md-8">
                占位
            </div>
        </div>
    </div>

    <div style="text-align: center;">
        Copyright 2023-now 挚夕导航 Designed by 挚夕
    </div>
</footer>

<!--/*/<th:block th:replace="fregments::script">/*/-->
<!-- jQuery (Bootstrap 的所有 JavaScript 插件都依赖 jQuery，所以必须放在前边) -->
<script src="../static/lib/jquery-1.12.4/jquery.min.js"></script>
<!-- 加载 Bootstrap 的所有 JavaScript 插件。你也可以根据需要只加载单个插件。 -->
<script src="../static/lib/bootstrap-3.4.1/js/bootstrap.min.js"></script>
<!--  bootstrap-dropdown-hover -->
<script src="../static/lib/bootstrap-dropdown-hover.js"></script>
<!--  二维码-->
<script src="../static/lib/qrcode/qrcode.min.js"></script>
<!--  图片裁剪-->
<script src="../static/lib/croptool.min.js"></script>
<script src="../static/lib/bootstrapValidator/bootstrapValidator.min.js"></script>
<script>
    $(function () {
        //bootstrap-dropdown-hover
        $('.navbar [data-toggle="dropdown"]').bootstrapDropdownHover({
            // see next for specifications
        });
        setTimeout(() => {
            $(".m-card").removeClass("lightSpeedIn");
        }, 3000);
        //特效
        $("li").hover(function () {
            $(this).toggleClass("m-up");
        })
        $(".m-eye").click(function () {
            //glyphicon-eye-open glyphicon-eye-close
            //更换图标
            console.log($(this).children('span').attr('class'))
            $(this).children('span').toggleClass("glyphicon-eye-open");
            $(this).children('span').toggleClass("glyphicon-eye-close");
            //修改input type
            console.log($(this).parent().children("input").attr('type'))
            if ($(this).parent().children("input").attr('type') === 'password') {
                $(this).parent().children("input").attr('type', 'text');
            } else {
                $(this).parent().children("input").attr('type', 'password');
            }
        });
    });
</script>
<!--/*/</th:block>/*/-->

<script>
    function initValidator() {
        $('#myModal').bootstrapValidator({
            excluded: [":disabled", ":hidden"], // 关键配置，表示只对于禁用域不进行验证，其他的表单元素都要验证
            message: 'This value is not valid',
            feedbackIcons: {
                validating: 'glyphicon glyphicon-refresh'
            },
            fields: {
                oldPassword: {
                    validators: {
                        notEmpty: {
                            message: '原密码不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 32,
                            message: '原密码必须在6到32位之间'
                        }
                    }
                },
                newPassword1: {
                    validators: {
                        notEmpty: {
                            message: '新密码不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 32,
                            message: '新密码必须在6到32位之间'
                        }
                    }
                },
                newPassword2: {
                    validators: {
                        notEmpty: {
                            message: '确认密码不能为空'
                        },
                        stringLength: {
                            min: 6,
                            max: 32,
                            message: '确认密码必须在6到32位之间'
                        },
                        identical: {
                            field: 'newPassword1',   //指定控件name
                            message: '两次密码不一致'
                        }
                    }
                }
            }
        });
    }

    function chooseImg(event) {
        var files = event.files || event.dataTransfer.files,
            file = files[0] || files;
        event.value = '';
        window.clip = new Clip({
            dragBoxClass: 'block',  //裁剪框类名
            clipRadio: 1 / 1,       //裁剪比例  宽/高  传0或空或不传等于不设置比例
            //单位px  仅为裁剪框的宽高  不等同裁剪后最终图片宽高
            initialHeight: 100,				//裁剪框初始高度
            initialWidth: 100,				//裁剪框初始宽度
            minHeight: 100,					//裁剪框最小高度
            minWidth: 100,					//裁剪框最小宽度
            maxWidth: 450,					//裁剪框最大宽度  不会大于裁剪区域宽度  取值需大于最小宽高
            maxHeight: 450,					//裁剪框最大高度  不会大于裁剪区域高度
            cornerColor: '#39f',			//裁剪框颜色
            encode: 'base64',				//文件类型
            type: 'png',					//保存图片类型
            name: 'img',					//文件名字
            quality: 1,					    //压缩质量
            //裁剪完成
            onDone: function (e) {
                // console.log(e);
                //return false;
                document.getElementById('selectAvatar').src = e;
            },
            onCancel: function () {

            }
        });
        clip.setSize(file);
    }

    function reloadScript() {
        setTimeout(() => {
            $(".m-card").removeClass("lightSpeedIn");
        }, 2000);
    }

    function loadAvatar() {
        $.ajax({
            type: 'get',
            headers: {
                'authorization': localStorage.getItem('token')
            },
            url: '[[@{/user/space/avatar}]]',
            success: function (data) {
                $("#userAvatar").html(data);
            }
        });
    }

    //点击菜单
    function clickItem(obj) {
        if ($(obj).hasClass('active')) {
            return;
        }
        var item = $(obj).data('item');
        $.ajax({
            type: 'post',
            url: '[[@{/user/space}]]',
            data: {
                item: item
            },
            headers: {
                'authorization': localStorage.getItem('token')
            },
            success: function (data) {
                //选中
                $(".list-group-item").removeClass('active');
                $(obj).addClass('active');
                //更新内容
                $("#content").html(data);
            },
            error: function (data) {
                toastr.info("加载失败", '提示');
            }
        })
    }

    $(function () {
        reloadScript();
        initValidator();
        //卡片特效
        $("#myContainer").on('mouseenter', '.m-card', function () {
            $(this).toggleClass("m-grow")
        });
        $("#myContainer").on('mouseleave', '.m-card', function () {
            $(this).toggleClass("m-grow")
        });
        //加载头像
        loadAvatar();
        //加载数据
        clickItem(document.getElementById("userInfo"));
        //自定义头像
        $("#myContainer").on('click', "#selectAvatar", function () {
            var inputAvatar = $("#inputAvatar");
            //选择图片文件
            inputAvatar.click();
        });
        $("#myContainer").on('change', '#radioSelectAvatar', function () {
            if ($("#radioSelectAvatar").prop('checked')) $("#selectAvatar").click();
        });

        //保存资料
        $("#myContainer").on('click', "#btnSaveInfo", function () {
            var nickname = $("#nickname").val().trim();
            var description = $("#description").val().trim();
            // console.log(nickname + description)
            var nicknameRegexpStr = /^.{2,10}$/;
            var desRegexpStr = /^.{1,30}$/;
            var nickRegexp = new RegExp(nicknameRegexpStr);
            var desRegexp = new RegExp(desRegexpStr);
            if (!nickRegexp.test(nickname)) {
                toastr.info('昵称长度2-10', '提示');
                return;
            }
            if (!desRegexp.test(description)) {
                toastr.info('个人描述长度1-30', '提示');
                return;
            }
            $.ajax({
                type: 'put',
                url: '[[@{/user/updateInfo}]]',
                data: {
                    nickname: nickname,
                    content: description
                },
                headers: {
                    'authorization': localStorage.getItem('token')
                },
                success: function (response) {
                    if (response.status == 'success') {
                        toastr.success(response.data, "提示");
                        //刷新页面
                        clickItem($("#userInfo"));
                    } else {
                        toastr.info(response.data, "提示")
                    }
                },
                error: function (response) {
                    toastr.error("服务器繁忙，请稍后再试", '错误');
                }
            });
        });


        $("#btn-sub").click(function () {
            var bootstrapValidator = $("#myModal").data("bootstrapValidator").validate();
            if (!bootstrapValidator.isValid()) return;
            var oldPassword = $("#oldPassword").val();
            var newPassword1 = $("#newPassword1").val();
            var newPassword2 = $("#newPassword2").val();
            console.log(oldPassword)
            console.log(newPassword1)
            console.log(newPassword2)
            $.ajax({
                type: 'put',
                url: '[[@{/user/changePassword}]]',
                headers: {
                    'authorization': localStorage.getItem('token')
                },
                data: {
                    oldPassword: oldPassword,
                    newPassword: newPassword1
                },
                success: function (response) {
                    if (response.status == 'success') {
                        toastr.success(response.data, '提示');
                        //关闭模态框
                        $("#myModal").modal('hide');
                    } else {
                        toastr.info(response.data, '操作失败');
                    }
                },
                error: function (response) {
                    toastr.error("服务器繁忙，请稍后再试", '错误');
                }
            })
        });
    });
</script>
</body>

</html>